/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.EnvironmentMonitor;
import me.zhengjie.modules.biz.domain.Lab;
import me.zhengjie.modules.biz.repository.LabRepository;
import me.zhengjie.modules.biz.service.dto.CabinetDto;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.repository.EnvironmentMonitorRepository;
import me.zhengjie.modules.biz.service.EnvironmentMonitorService;
import me.zhengjie.modules.biz.service.dto.EnvironmentMonitorDto;
import me.zhengjie.modules.biz.service.dto.EnvironmentMonitorQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.EnvironmentMonitorMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Admin
* @date 2023-02-17
**/
@Service
@RequiredArgsConstructor
public class EnvironmentMonitorServiceImpl implements EnvironmentMonitorService {

    private final EnvironmentMonitorRepository environmentMonitorRepository;
    private final EnvironmentMonitorMapper environmentMonitorMapper;
    private final LabRepository labRepository;

    @Override
    public Map<String,Object> queryAll(EnvironmentMonitorQueryCriteria criteria, Pageable pageable){
        Page<EnvironmentMonitor> page = environmentMonitorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        Page<EnvironmentMonitorDto> dto = page.map(environmentMonitorMapper::toDto);
        List<EnvironmentMonitorDto> resultDto = dto.getContent();
        for (EnvironmentMonitorDto environmentMonitorDto:resultDto) {
            Lab lab = labRepository.findById(environmentMonitorDto.getLabId()).orElseGet(Lab::new);
            environmentMonitorDto.setLabName(lab.getName());
            String adminName = labRepository.findByadminId(environmentMonitorDto.getAdministrator()).get("adminName");
            environmentMonitorDto.setAdminName(adminName);
        }
        return PageUtil.toPage(dto);
    }

    @Override
    public List<EnvironmentMonitorDto> queryAll(EnvironmentMonitorQueryCriteria criteria){
        List<EnvironmentMonitor> environmentMonitorList = environmentMonitorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
        List<EnvironmentMonitorDto> environmentMonitorDtoList = environmentMonitorMapper.toDto(environmentMonitorList);
        for (EnvironmentMonitorDto environmentMonitorDto : environmentMonitorDtoList) {
            Lab lab = labRepository.findById(environmentMonitorDto.getLabId()).orElseGet(Lab::new);
            environmentMonitorDto.setLabName(lab.getName());
            String adminName = labRepository.findByadminId(environmentMonitorDto.getAdministrator()).get("adminName");
            environmentMonitorDto.setAdminName(adminName);
        }
        return environmentMonitorDtoList;
    }

    @Override
    @Transactional
    public EnvironmentMonitorDto findById(Integer id) {
        EnvironmentMonitor environmentMonitor = environmentMonitorRepository.findById(id).orElseGet(EnvironmentMonitor::new);
        ValidationUtil.isNull(environmentMonitor.getId(),"EnvironmentMonitor","id",id);
        return environmentMonitorMapper.toDto(environmentMonitor);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public EnvironmentMonitorDto create(EnvironmentMonitor resources) {
        return environmentMonitorMapper.toDto(environmentMonitorRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(EnvironmentMonitor resources) {
        EnvironmentMonitor environmentMonitor = environmentMonitorRepository.findById(resources.getId()).orElseGet(EnvironmentMonitor::new);
        ValidationUtil.isNull( environmentMonitor.getId(),"EnvironmentMonitor","id",resources.getId());
        environmentMonitor.copy(resources);
        environmentMonitorRepository.save(environmentMonitor);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            environmentMonitorRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<EnvironmentMonitorDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (EnvironmentMonitorDto environmentMonitor : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", environmentMonitor.getName());
            map.put("实验室", environmentMonitor.getLabName());
            map.put("负责人", environmentMonitor.getAdminName());
            map.put("温度", environmentMonitor.getTemperature());
            map.put("温度阈值", environmentMonitor.getTemperatureThresholdValue());
            map.put("湿度", environmentMonitor.getHumidity());
            map.put("湿度阈值", environmentMonitor.getHumidityThresholdValue());
            map.put("压差", environmentMonitor.getPressure());
            map.put("压差阈值", environmentMonitor.getPressureThresholdValue());
            map.put("报警信息", environmentMonitor.getErrorMessage());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
